<?php
namespace Account\Controller;

use Common\Controller\ApiController;
use Util\ModelUtils;

class AaOrderController extends ApiController
{
    public function store()
    {
        $user = $this->user;
        $inputs = $this->request();
        $mAaOrder = D('AaOrder');
        $data = [
            'ad_id' => $user['id'],
            'anchor_id' => $inputs['anchor_id'],
            'phone' => $inputs['phone'],
            'remark' => $inputs['remark'],
            'realname' => $inputs['realname'],
        ];
        if ($mAaOrder->create($data)) {
            $id = $mAaOrder->add();
            $aaOrder = D('AaOrder')->find($id);
            ModelUtils::belongsTo($aaOrder, ['relation' => 'Account/Users', 'foreignKey' => 'ad_id', 'refKey' => 'user_id', 'name' => 'provider']);
            ModelUtils::belongsTo($aaOrder, ['relation' => 'Account/Users', 'foreignKey' => 'anchor_id', 'refKey' => 'user_id', 'name' => 'anchor']);
            ModelUtils::manyToMany($aaOrder['anchor'], ['Users.user_id', 'AnchorTags.user_id', 'LiveTag.id', 'AnchorTags.tag_id'], 'tags');
            $aaOrder = transform('AaOrder', 'detail', $aaOrder);
            $this->response($aaOrder);
        } else {
            $this->errorResponse(10012, $mAaOrder->getError());
        }
    }

    public function show($id)
    {
        $aaOrder = D('AaOrder')->find($id);
        if (empty($aaOrder)) {
            $this->errorResponse(10000);
        }
        ModelUtils::belongsTo($aaOrder, ['relation' => 'Account/Users', 'foreignKey' => 'ad_id', 'refKey' => 'user_id', 'name' => 'provider']);
        ModelUtils::belongsTo($aaOrder, ['relation' => 'Account/Users', 'foreignKey' => 'anchor_id', 'refKey' => 'user_id', 'name' => 'anchor']);
        ModelUtils::manyToMany($aaOrder['anchor'], ['Users.user_id', 'AnchorTags.user_id', 'LiveTag.id', 'AnchorTags.tag_id'], 'tags');
        $aaOrder = transform('AaOrder', 'detail', $aaOrder);
        $this->response($aaOrder);
    }

    public function check()
    {
        $id = I('get.id');
        $input = $this->request();
        $operation = $input['operation'];
        $order = D('AaOrder')->where(['status' => 'pending', 'id' => $id, 'anchor_id' => $this->user['id']])->find();
        if (empty($order)) $this->errorResponse(10000);
        $order['status'] = $operation;
        D('AaOrder')->save($order);
        $this->modifySuccessResponse();
        
    }

    public function orders()
    {
        $input = $this->request();
        $page = (int)filter($input['page'], 1);
        $perPage = (int)filter($input['per_page'], 10);
        $status = filter($input['status'], 'all');

        $user = D('Users')->find($this->user['id']);
        $where = [];
        if ($status != 'all') {
            $where['status'] = $status;
        }
        if ($user['role'] == '3') {
            $where['anchor_id'] = $this->user['id'];
        } elseif ($user['role'] == '2') {
            $where['ad_Id'] = $this->user['id'];
        } else {
            $this->errorResponse(10010);
        }
        $count = D('AaOrder')->where($where)->count();
        $orders = D('AaOrder')->where($where)
            ->order('create_time desc')
            ->page($page, $perPage)
            ->select();
        ModelUtils::belongsTo($orders, ['relation' => 'Account/Users', 'foreignKey' => 'ad_id', 'refKey' => 'user_id', 'name' => 'provider']);
        ModelUtils::belongsTo($orders, ['relation' => 'Account/Users', 'foreignKey' => 'anchor_id', 'refKey' => 'user_id', 'name' => 'anchor']);
        ModelUtils::manyToMany(array_column_link($orders, 'anchor'), ['Users.user_id', 'AnchorTags.user_id', 'LiveTag.id', 'AnchorTags.tag_id'], 'tags');
        $orders = transform('AaOrder', 'detail', $orders);
        $pagination = pagination($count, $perPage, $page);
        $this->response($orders, $pagination);

    }

}
